import { MSG_TYPES } from '../../utils/MESSAGE_TYPES'

export default function (xhr, PROXY_ROUTES) {
  return function (body) {
    const { url, send, bgReqId: id } = xhr
    // 判断是否需要代理
    if (PROXY_ROUTES.filter(({ enable }) => enable).some(({ matching }) => {
      const REG = new RegExp(matching, 'g')
      return REG.test(url)
    })) {
      window.postMessage({
        event: MSG_TYPES.send,
        id,
        data: {
          body
        }
      })
    } else {
      send.call(xhr, body)
    }
  }
}
